 /*-----------------------------------------------File Info------------------------------------------------
** File Name:               userConfig.h  
** Last modified date:      2016.09.8
** Last version:            V0.1
** Description:             
**
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.8
** author:                  YLY
** Version:                 V0.1
** Descriptions:            应用程序配置
**--------------------------------------------------------------------------------------------------------*/

#ifndef __CONFIG_H_
#define __CONFIG_H_

#include <stdio.h>

#include "bspConfig.h"
#include "bspFMC.h"
#include "bspUART.h"
#include "bspSPI.h"
#include "bspI2C.h"
#include "bspADC.h"
#include "bspTimer.h"


#define DBG_CLOSE			0
#define DBG_ERR				1
#define DBG_DBG				2
#define GLOBAL_DBG_LEVEL	DBG_DBG

#if GLOBAL_DBG_LEVEL == DBG_DBG
	#define GLOBAL_DBG	printf
	#define GLOBAL_ERR	printf	
#elif GLOBAL_DBG_LEVEL == DBG_ERR
	#define GLOBAL_ERR	printf
#else
	#define GLOBAL_DBG(...)	
	#define GLOBAL_ERR(...)	
#endif

/* 升级渠道
*  RV1109   MCU_DITCH_“IDENTIFIER”
*/
#define   IDENTIFIER				3U		/* AP程序渠道号,升级类型（渠道号）为"MCU_DITCH_+IDENTIFIER",M058门口机为0,GDE230为3,M058门禁机为1*/

#define   RUNNING_ENV_APROM			1U		/* 程序运行环境为APROM下运行 */
#define   RUNNING_ENV_LDROM			2U		/* 程序运行环境为LDROM下运行 */
#define   MCU_TYPE				 	BSP_MCU_TYPE

/*  版本号 */
#define   BOOTLOAD_VER				"V0.0.2-IIC"


#define INT_OUT			GPIOB,GPIO_PIN_2		//PB2
#define MCU_LED			GPIOB,GPIO_PIN_13		//PB13


//------------GPIO操作函数定义---------------
#define LED_ON()					gpio_bit_write(MCU_LED,1);
#define LED_OFF()					gpio_bit_write(MCU_LED,0)
#define LED_TOGGLE()    			gpio_bit_toggle(MCU_LED)

#define KEY_LED_ON()				gpio_bit_write(KEY_LED, 0);
#define KEY_LED_OFF()				gpio_bit_write(KEY_LED, 1)
#define KEY_LED_TOGGLE()			gpio_bit_toggle(KEY_LED)

#define RFID_LED_ON()				gpio_bit_write(RFID_LED, 0)		//gpio_bit_set(GPIOB,GPIO_PIN_15)
#define RFID_LED_OFF()				gpio_bit_write(RFID_LED, 1)		//gpio_bit_reset(GPIOB,GPIO_PIN_15)
#define RFID_LED_TOGGLE()			gpio_bit_toggle(RFID_LED)


//----------------FLASH分配-------------------------------------
#define FLASH_BASE_ADDR 		FLASH_BASE	//0x08000000u
#define FLASH_SIZE				0x10000		//64K
#define FLASH_END_ADDR 			(FLASH_BASE + FLASH_SIZE)

#define BOOTLOAD_BASE_ADDR 		FLASH_BASE_ADDR	
#define BOOTLOAD_SIZE			0x4000		//16K
#define BOOTLOAD_END_ADDR 		(BOOTLOAD_BASE_ADDR + BOOTLOAD_SIZE)

#define USER_DATA_SIZE			(FMC_PAGE_SIZE*2)	//2K
#define USER_DATA_ADDR			(uint32_t)(FLASH_END_ADDR - BOOTLOAD_SIZE)	//密码存储位置

#define APP_FLASH_BASE     	 	(BOOTLOAD_BASE_ADDR + BOOTLOAD_SIZE)
#define	APP_FLASH_SIZE		 	(FLASH_SIZE - BOOTLOAD_BASE_ADDR - USER_DATA_SIZE)	//64-16-2	46K
#define APP_FLASH_END        	(APP_FLASH_BASE + APP_FLASH_SIZE)

#define STACK_ADDR			    0x2				
#define USER_DATA_MARK			0x55ul		//用户数据使用标识


#define   WAIT_TIMEOUT			4000		//等待超时4S	
//定义常用标识
/*  返回状态定义 */
#define SUCCESS 	true 		
#define FAIL    	false


typedef enum enumUpdateSta
{
	APROM_NONE_UPDATE = 0,
	LDROM_NONE_UPDATE,
	LDROM_UIPDATEING,
}teUpdateSta;


typedef struct	/* 为了升级做兼容,需要保证结构体成员顺序和类型不能变. */
{
	uint8_t 	userMark;					//FLASH初始化标识
	uint8_t		reserv11;					//保留
	uint8_t		reserv12;					//保留
	uint8_t		reserv13;					//保留
	
	uint8_t 	apromVerArch;				//APROM程序架构版本号
	uint8_t 	apromVerSoft;				//APROM程序版本号
	uint8_t 	apromVerModify;				//APROM程序修订版本号
	uint8_t 	identifier;					//AP程序渠道号
	
	teUpdateSta apromUpdateSta;				//标识升级状态
	uint8_t		rfidType;					//rfid类型
	uint8_t		reserv21;					//保留
	uint8_t		reserv22;					//保留
	
	uint8_t 	ssidLen;					//保存SSID长度		
	uint8_t 	relayType;					//继电器类型	0常开，1常闭
	uint8_t 	lockType;					//锁类型
	uint8_t 	relayTime;					//继电器吸合时间	单位秒

	uint8_t 	wifiOrBleInitFlg;			//模块初始化标识
	uint8_t		wifiOrBleInitCount;			//模块初始化计数
	uint8_t		reserv31;					//保留
	uint8_t		reserv32;					//保留
	
	uint32_t 	lockTime;					//开锁时间		//单位mS or S
	uint32_t 	lockTimeout;				//开锁超时时间	//单位S
	uint32_t	reserv30;					//保留
	uint32_t	reserv41;					//保留
	uint32_t	reserv42;					//保留
	
	uint32_t 	baudRate;					//RFID串口波特率
	uint8_t 	hostPid[4];					//门口机PID，用于WIFI开门
	uint8_t 	password[6];				//系统密码		
	char 		hostSsid[4*21];				//SSID，UTF－8格式，长度不能超过20个汉字	
}tsUserData;

typedef enum
{
	ISP_IDLE,			//空闲
	ISP_REQUEST,		//请求升级
	ISP_UPDATEING,		//升级中
	ISP_FINISH,			//升级完成
}teStatus;

typedef struct strIsp
{
	uint32_t time;	/* 等待升级超时 */
	teStatus status;
}tsIsp;


#endif	/* __CONFIG_H_ */


